<?php
/**
 * Created by PhpStorm.
 * User: 柳迪
 * Date: 2018/7/12
 * Time: 8:24
 */

namespace app\api\controller;


use think\Controller;
use think\Session;

class Activities extends Controller
{
    public function getList()
    {
        $result=db('activities')->field('aid,aname,apic,aaddtime,astarttime,aintro,aprice,aaddress,alatitude,alongitude')->limit(6)->order('aflag desc,aaddtime desc')->select();
        if($result)
        {
            echo json_encode($result);
            die();
        }
        else
        {
            echo json_encode(['status'=>0,'message'=>'数据暂无']);
        }
    }

    public function getDetail()
    {
        if(input('?id'))
        {
            $aid=input('id');
            $result=db('activities')->field('aid,aname,apic,aaddtime,astarttime,aintro,aprice,aaddress,alatitude,alongitude')->find($aid);
            if($result)
            {
                if(Session::has('uid'))
                {
                    $result_select=db('enroll')->where(['aid'=>$aid,'uid'=>Session::get('uid')])->find();
                    if($result_select)
                        $result['flag']=1;
                    else
                        $result['flag']=0;
                }
                else
                    $result['flag']=0;
                echo json_encode($result);
                die();
            }
            else
            {
                echo json_encode(['status'=>0,'message'=>'id错误']);
                die();
            }
        }
        else
        {
            echo json_encode(['status'=>0,'message'=>'参数错误']);
            die();
        }
    }

    public function enroll()
    {
        if(Session::has('uid'))
        {
            if(input('?id'))
            {
                $aid=input('id');
                $result=db('enroll')->where(['aid'=>$aid,'uid'=>Session::get('uid')])->find();
                if($result)
                {
                    echo json_encode(['status'=>0,'message'=>'您已报名此项目，无法重复报名。']);
                    die();
                }
                $result_activities=db('activities')->field('aprice')->find($aid);
                if(!$result_activities)
                {
                    echo json_encode(['status'=>0,'message'=>'参数id错误。']);
                    die();
                }
                $result_insert=db('enroll')->insert(['aid'=>$aid,'eprice'=>$result_activities['aprice'],'uid'=>Session::get('uid'),'etime'=>date("Y-m-d H:i")]);
                if($result_insert)
                {
                    echo json_encode(['status'=>1,'message'=>'报名成功。']);
                    die();
                }
                else
                {
                    echo json_encode(['status'=>0,'message'=>'未知错误。']);
                    die();
                }
            }
        }
        else
        {
            echo json_encode(['status'=>0,'message'=>'尚未登陆，无法报名。']);
            die();
        }
    }

    public function cancel()
    {
        if(!Session::has('uid'))
        {
            echo json_encode(['status'=>0,'message'=>'尚未登陆。']);
            die();
        }
        if(input('?id'))
        {
            $aid=input('id');
            $uid=Session::get('uid');
            $data['uid']=$uid;
            $data['aid']=$aid;
            $result=db('enroll')->where($data)->find();
            if($result)
            {
                db('enroll')->delete($result['eid']);
                echo json_encode(['status'=>1,'message'=>'取消成功。']);
                die();
            }
            else
            {
                echo json_encode(['status'=>0,'message'=>'尚未报名。']);
                die();
            }
        }
    }
}